package com.example.test1.service.impl;

import com.example.test1.model.Drink;
import com.example.test1.repository.DrinkRepository;
import com.example.test1.service.IDrinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DrinkServicelmpl implements IDrinkService {

    @Autowired
    private DrinkRepository drinkRepository;

    @Override
    public List<Drink> getAllDrinks() {
        return drinkRepository.findAll(Sort.by(Sort.Direction.DESC,"createTime"));
    }

    @Override
    public Drink addNewDrink(Drink drink) {
        return drinkRepository.saveAndFlush(drink);
    }

    @Override
    public Optional<Drink> getDrinkById(Long id) {
        return drinkRepository.findById(id);
    }

    @Override
    public void deleteDrink(Long id) {
        drinkRepository.deleteById(id);
    }

    @Override
    public void updateDrink(Drink drink) {
        drinkRepository.save(drink);
    }


}
